﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Mybook.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace MyStory.Service
{
    public class FictionService
    {
        private HttpClient _client;

        public FictionService()
        {
            _client = new HttpClient();
        }


        public async Task<List<FictionModel>> GetRepositoriesAsync(string uri)
        {
            List<FictionModel> repositories = null;
            try
            {
                HttpResponseMessage response = await _client.GetAsync(uri);
  

                if (response.IsSuccessStatusCode)
                {
                    string content = await response.Content.ReadAsStringAsync();
                    JObject jsonObj = JObject.Parse(content);
                    string data = jsonObj["data"].ToString();
                    //JObject chapjson=JObject.Parse(data);
                    //string chapterdetailjson=chapjson["chapterList"].ToString();

                    //  repositories = JsonConvert.DeserializeObject(content[3].ToString()) as List<FictionModel>;
                    repositories = JsonConvert.DeserializeObject<List<FictionModel>>(data);
                    if (repositories != null)
                    {
                        foreach (var items in repositories)
                        {
                            items.ImageUrl = items.ImageUrl.Insert(4, "s");
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("\tERROR {0}", ex.Message);
            }

            return repositories;
        }

        public async Task<List<FictionModel>> GetChapterRepositoriesAsync(string uri)
        {
            List<FictionModel> repositories = null;
            try
            {
                HttpResponseMessage response = await _client.GetAsync(uri);


                if (response.IsSuccessStatusCode)
                {
                    string content = await response.Content.ReadAsStringAsync();
                    JObject jsonObj = JObject.Parse(content);
                    string data = jsonObj["data"].ToString();
                    JObject chapjson = JObject.Parse(data);
                    string chapterdetailjson = chapjson["chapterList"].ToString();

                    repositories = JsonConvert.DeserializeObject<List<FictionModel>>(chapterdetailjson);
                    //if (repositories != null)
                    //{
                    //    foreach (var items in repositories)
                    //    {
                    //        //JObject jsonj = JObject.Parse(items.ToString());
                    //        //items. = GetNestJsonValue(jsonj.Children(), "login");
                    //        items.ImageUrl = items.ImageUrl.Insert(4, "s");
                    //    }
                    //}

                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("\tERROR {0}", ex.Message);
            }

            return repositories;
        }

        public async Task<string> GetContentRepositoriesAsync(string uri)
        {
            string repositories = null;
            try
            {
                HttpResponseMessage response = await _client.GetAsync(uri);


                if (response.IsSuccessStatusCode)
                {
                    string content = await response.Content.ReadAsStringAsync();
                    JObject jsonObj = JObject.Parse(content);
                    string jjj = jsonObj["data"].ToString();
                    string[] strarray = jjj.Split(new char[4] { '\"',',','[',']'});
                    StringBuilder stringBuilder = new StringBuilder();
                    foreach (string str in strarray)
                    {
                        stringBuilder.Append(str);
                    }

                    repositories = stringBuilder.ToString();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("\tERROR {0}", ex.Message);
            }

            return repositories;
        }

        public string GetNestJsonValue(JEnumerable<JToken> jToken, string key)
        {
            IEnumerator enumerator = jToken.GetEnumerator();
            while (enumerator.MoveNext())
            {
                JToken jc = (JToken)enumerator.Current;
                if (jc is JObject || ((JProperty)jc).Value is JObject)
                {
                    return GetNestJsonValue(jc.Children(), key);
                }
                else
                {
                    if (((JProperty)jc).Name == key)
                    {
                        return ((JProperty)jc).Value.ToString();
                    }
                }
            }

            return null;
        }
    }
}